package controller;

import java.util.Calendar;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

import config.model.Changepeople;
import config.model.Claim;
import config.model.Project;
import config.model.User;
import service.AnnouncementService;
import service.ChangeService;
import service.ProjectService;

/**
 * 学生页面控制类
 * 
 * @author 陈凌志
 *
 */

public class StudentController extends Controller{

	
	AnnouncementService as = new AnnouncementService();
	ProjectService ps = new ProjectService();
	ChangeService cs = new ChangeService();
	
	/**
	 * 访问学生主页面方法
	 * 
	 */
	public void index() {
		int pageNumber = Integer.parseInt(getPara("pageNumber"));
		setAttr("title","公告信息");
		setAttr("announcementPage",as.queryAnnouncementPage(pageNumber));
		render("index.html");
	}
	
	/**
	 * 申报项目页面访问方法
	 * 
	 */
	public void application () {
		setAttr("title","项目申报");
		setSessionAttr("thisYear", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
		render("application.html");
	}
	
	/**
	 * 申报项目方法
	 * 项目相关文件储存于file/project/(项目id)文件下
	 * 
	 */
	public void getProject() {
		String projectId = ps.lsID();
		try{
			UploadFile file=getFile("file","project/"+projectId+"/application",1024*1024*1024,"utf-8");
			Project project = getModel(Project.class);
			if(file!=null) 
				project.setFile(file.getOriginalFileName());
			String msg = ps.addProject(project,projectId);
			if(msg.equals("ok")) {
				setAttr("title","申报成功");
				render("success.html");
			}else{
				setAttr("title",msg);
				render("success.html");
			}
			
		}catch(Exception e){
			setAttr("title","申报失败");
			render("success.html");
		}
	}
	
	/**
	 * 所负责项目信息页面访问方法
	 * 
	 */
	public void myProjectInfo () {
		User u = getSessionAttr("User");
		List<Record> project = ps.queryProjectByPersonsResponsibleName(u.getName());
		setAttr("title","所负责项目信息");
		setAttr("project",project);
		setAttr("file",ps.getFileName(project,"application"));
		setAttr("endfile",ps.getFileName(project,"end"));
		render("myProjectInfo.html");
	}
	
	/**
	 * 所参与项目信息页面访问方法
	 * 
	 */
	public void participate() {
		User u = getSessionAttr("User");
		List<Record> project = ps.queryParticipate(u.getName(), u.getUserName());
		setAttr("title","所参与项目信息");
		setAttr("project",project);
		setAttr("file",ps.getFileName(project,"application"));
		setAttr("endfile",ps.getFileName(project,"end"));
		render("myProjectInfo.html");
	}
	
	/**
	 * 添加新文件方法
	 * 项目相关文件储存于file/project/(项目id)/application文件下
	 * 
	 */
	public void addFile() {
		try{
			String projectID = getPara("projectID");
			getFile("file","project/"+projectID+"/application",1024*1024*1024,"utf-8");
			ps.fileToZip(projectID,"application");
			setAttr("title","文件上传成功");
			render("success.html");
		}catch(Exception e){
			setAttr("title","文件上传失败");
			render("success.html");
		}
		
	}
	
	/**
	 * 团队成员变更申请页面访问方法
	 * 
	 */
	public void changeOtherPeople () {
		User u = getSessionAttr("User");
		List<Record> project = ps.queryProjectByPersonsResponsibleName(u.getName());
		List<Record> change = cs.queryChangePeople(u.getName());
		setAttr("projectID",project.get(0).get("projectId"));
		setAttr("title","团队成员变更申请");
		setAttr("project",project);
		setAttr("change",change);
		render("changePeople.html");
	}
	
	/**
	 * 教师变更申请页面访问方法
	 * 
	 */
	public void changeTeacher () {
		User u = getSessionAttr("User");
		List<Record> project = ps.queryProjectByPersonsResponsibleName(u.getName());
		List<Record> change = cs.queryChangePeople(u.getName());
		setAttr("title","教师变更申请");
		setAttr("project",project);
		setAttr("change",change);
		render("changePeople.html");
	}
	
	/**
	 * 人员变更申请提交方法
	 * 
	 */
	public void changePeople() {
		boolean flag = cs.addChangePeople(getModel(Changepeople.class));
		if(flag == true) {
			setAttr("title","提交申请成功");
			render("success.html");
		}else {
			setAttr("title","提交申请失败");
			render("success.html");
		}
		
	}
	
	/**
	 * 结项申报页面访问方法
	 * 
	 */
	public void end () {
		User u = getSessionAttr("User");
		List<Record> project = ps.queryProjectByPersonsResponsibleName(u.getName());
		setAttr("title","结项申请");
		setAttr("project",project);
		setAttr("file",ps.getFileName(project,"application"));
		setAttr("endfile",ps.getFileName(project,"end"));
		render("end.html");
	}
	
	
	/**
	 * 添加新文件方法
	 * 项目相关文件储存于file/project/(项目id)/end文件下
	 * 
	 */
	public void endFile() {
		try{
			String projectID = getPara("projectID");
			getFile("file","project/"+projectID+"/end",1024*1024*1024,"utf-8");
			String paper = getPara("paper");
			ps.fileToZip(projectID,"end");
			if(!ps.queryProjectByProjectId(projectID).get(0).get("status").equals("待审核立项")) {
				if(ps.End("待审核结项", projectID,paper)) {
					setAttr("title","文件上传且申报成功");
					render("success.html");
				}else {
					setAttr("title","申报失败");
					render("success.html");
				}
			}else {
				setAttr("title","文件上传成功");
				render("success.html");
			}
		}catch(Exception e){
			setAttr("title","文件上传或申报失败");
			render("success.html");
		}
		
	}
	
	/**
	 * 报销申报页面访问方法
	 * 
	 */
	public void claim () {
		User u = getSessionAttr("User");
		List<Record> project = ps.queryProjectByPersonsResponsibleName(u.getName());
		setAttr("title","报销申请");
		setAttr("project",project);
		setAttr("claimfile",ps.getFileName(project,"claim"));
		render("claim.html");
	}
	
	/**
	 * 添加新文件方法
	 * 项目相关文件储存于file/project/(项目id)/claim文件下
	 * 
	 */
	public void claimFile() {
		try{
			String projectID = getPara("projectID");
			getFile("file","project/"+projectID+"/claim",1024*1024*1024,"utf-8");
			Claim claim = getModel(Claim.class);
			ps.fileToZip(projectID,"claim");
			if(ps.addClaim(claim)) {
				setAttr("title","文件上传且申报成功");
				render("success.html");
			}else {
				setAttr("title","申报失败");
				render("success.html");
			}
		}catch(Exception e){
			setAttr("title","文件上传或申报失败");
			render("success.html");
		}
		
	}
	
	/**
	 * 退出登录方法
	 * 
	 */
	public void signOut() {
		removeSessionAttr("User");
		redirect("/index");
	}
}
